﻿
using Admin.NET.Plugin.ApprovalFlow.Service.ApprovalflowGroup.Dto;

namespace Admin.NET.Plugin.ApprovalFlow.Service.ApprovalflowGroup;

/// <summary>
/// 审批流分组表服务
/// </summary>
[ApiDescriptionSettings(ApprovalFlowConst.GroupName, Order = 100)]
public class ApprovalflowGroupService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<ApprovalFlowGroup> _approvalflowGroupRep;
    private readonly SqlSugarRepository<ApprovalFlow> _approvalflowRep;
    public ApprovalflowGroupService(SqlSugarRepository<ApprovalFlowGroup> approvalflowGroupRep,SqlSugarRepository<ApprovalFlow> approvalFlowRep)
    {
        _approvalflowGroupRep = approvalflowGroupRep;
        _approvalflowRep=approvalFlowRep;
    }

    /// <summary>
    /// 分页查询审批流分组表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<ApprovalflowGroupOutput>> Page(PageApprovalflowGroupInput input)
    {
        input.SearchKey = input.SearchKey?.Trim();
        var query = _approvalflowGroupRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.GroupName.Contains(input.SearchKey)
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.GroupName), u => u.GroupName.Contains(input.GroupName.Trim()))
            .Select<ApprovalflowGroupOutput>();
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }
    /// <summary>
    /// 获取审批流TreeList
    /// </summary>
    /// <returns></returns>
    public async Task<List<ApprovalGroupTreeDto>> GetFlowGroupTreeList()
    {
        List<ApprovalGroupTreeDto> approvalGroupTrees = new List<ApprovalGroupTreeDto>();
        var res= await _approvalflowGroupRep.AsQueryable()           
            .RightJoin<ApprovalFlow>((afg,af)=>afg.Id==af.GroupId)
            .Where((afg, af) => af.IsDelete == false && afg.IsDelete == false)
            .Select((afg,af)=>new ApprovalGroupTreeDto() {
               //Gid = afg.Id,
               GroupId = af.GroupId,
               Code = af.Code,
               GroupName=afg.GroupName,
               Icon=af.Icon,
               Id=af.Id,
               Name=af.Name,
               FlowJson=af.FlowJson,
               FormJson=af.FormJson

            })           
            .ToListAsync();
        var groupList=res.GroupBy(it => it.GroupId);
        foreach(var group in groupList)
        {             
            int i = 0;
            ApprovalGroupTreeDto approvalGroupTreeDto = new ApprovalGroupTreeDto();
            foreach (var item in group)
            {
                
                if (i==0)
                {                     
                     item.Adapt(approvalGroupTreeDto);
                }               
                ApprovalGroupTreeDto child = new ApprovalGroupTreeDto();
                item.Adapt(child);
                approvalGroupTreeDto.Child.Add(child);
                i++;
                //foreach (var item2 in res)
                //{                   
                //if (item.Id != item2.Id)
                //{
                //if (item.GroupId == item2.GroupId)
                //{

                //}
                //}

                //}
              
            }
            approvalGroupTrees.Add(approvalGroupTreeDto);
        }      
        return approvalGroupTrees;
        
    }

    /// <summary>
    /// 增加审批流分组表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task<long> Add(AddApprovalflowGroupInput input)
    {
        var entity = input.Adapt<ApprovalFlowGroup>();
        await _approvalflowGroupRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除审批流分组表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]  
    public async Task Delete(DeleteApprovalflowGroupInput input)
    {
        var entity = await _approvalflowGroupRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _approvalflowGroupRep.FakeDeleteAsync(entity);   //假删除
        //await _approvalflowGroupRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新审批流分组表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateApprovalflowGroupInput input)
    {
        var entity = input.Adapt<ApprovalFlowGroup>();
        await _approvalflowGroupRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取审批流分组表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<ApprovalFlowGroup> Detail([FromQuery] QueryByIdApprovalflowGroupInput input)
    {
        return await _approvalflowGroupRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取审批流分组表列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<ApprovalflowGroupOutput>> List([FromQuery] PageApprovalflowGroupInput input)
    {
        return await _approvalflowGroupRep.AsQueryable().Select<ApprovalflowGroupOutput>().ToListAsync();
    }





}
